﻿using DAL;
using Model;
using Newtonsoft.Json;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FubiManger.Controls;

namespace FubiManger.Pad
{
    partial class PorderfoodForm : Smobiler.Core.Controls.MobileForm
    {
        /// <summary>
        /// 平板下单
        /// </summary>
        public PorderfoodForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 用户ID
        /// </summary>
        public string adminid { get; set; }
        /// <summary>
        /// 商品集合
        /// </summary>
        List<ms_goods> gdlist = new List<ms_goods>();
        /// <summary>
        /// 购物车
        /// </summary>
        List<Gdpro> carlist = new List<Gdpro>();
        /// <summary>
        /// 购买数量
        /// </summary>
        int buycount = 0;
        /// <summary>
        /// 金额
        /// </summary>
        double paymoney = 0;
        /// <summary>
        /// 桌号
        /// </summary>
        public string disksn { get; set; }
        private string u_sage = "1";
        /// <summary>
        /// 餐桌集合
        /// </summary>
        List<ms_disk> disklist = new List<ms_disk>();
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Form.Close();
        }

        private void PorderfoodForm_Load(object sender, EventArgs e)
        {
            //Choosecontrol ccon = new Choosecontrol();
            //this.Form.ShowDialog(ccon, (obj, args) =>
            //{
            //    if (!string.IsNullOrEmpty(ccon.u_sage))
            //        u_sage = ccon.u_sage;
            disklist = diskDal.Querybymerid(merchantid);
            int dcount = 0;
            if (disklist.Count % 2 == 0)
            {
                dcount = disklist.Count / 2;
            }
            else
                dcount = disklist.Count + 1;
            pdisks.Height = dcount * 55;
            for (int i = 0; i < disklist.Count; i++)
            {
                diskmenucontrol con = new diskmenucontrol();
                con.diskname = disklist[i].ds_disknmae;//位置名称
                con.diskid = disklist[i].ds_id;//位置ID
                pdisks.Controls.Add(con);
            }
            List<ms_classify> clist = classifyDal.Querybymerid(merchantid);//商家ID
            pmenu.Height = clist.Count * 40;
            int num = 0;
            if (clist.Count > 0)
            {
                num = 1;
                Pmenucontrol con = new Pmenucontrol();
                con.businname = clist[0].title;
                con.clsid = clist[0].id;
                Button btn = (Button)con.Controls.Find("btnvalue");
                btn.BackColor = System.Drawing.Color.FromArgb(207, 29, 32);
                btn.ForeColor = System.Drawing.Color.White;
                pmenu.Controls.Add(con);
                for (int i = num; i < clist.Count; i++)
                {
                    Pmenucontrol con1 = new Pmenucontrol();
                    con1.businname = clist[i].title;
                    con1.clsid = clist[i].id;
                    con1.stype = "1";//下单
                    con1.Controls.Find("btnvalue").BackColor = System.Drawing.Color.Transparent;
                    pmenu.Controls.Add(con1);
                }
                gdlist = goodsDal.Querybyclassid(clist[0].id, merchantid);//通过分类ID查商品列表
                int count = 0;
                if (gdlist.Count % 5 == 0)
                    count = gdlist.Count / 5;
                else
                    count = gdlist.Count / 5 + 1;
                ppros.Height = count * 175;
                for (int i = 0; i < gdlist.Count; i++)
                {
                    Pprocontrol pcon = new Pprocontrol();
                    pcon.cover = gdlist[i].cover;//封面
                    pcon.stype = "2";//下单
                    pcon.describes = gdlist[i].describes;//描述
                    pcon.gdname = gdlist[i].goods_name;//商品名称
                    pcon.price = Convert.ToDouble(gdlist[i].price);//销售价
                    pcon.market_price = "￥" + gdlist[i].market_price;//市场价
                    pcon.gdid = gdlist[i].id;//商品ID
                    ppros.Controls.Add(pcon);
                }
            }
            //});



        }
        /// <summary>
        /// 根据菜单改变内容
        /// </summary>
        /// <param name="name"></param>
        /// <param name="clsid"></param>
        public void Show_name(string name, string clsid)
        {

            if (ppros.Controls.Count > 0)
                ppros.Controls.Clear();
            gdlist = goodsDal.Querybyclassid(clsid, merchantid, u_sage);//通过分类ID查商品列表
            int count = 0;
            if (gdlist.Count % 5 == 0)
                count = gdlist.Count / 5;
            else
                count = gdlist.Count / 5 + 1;
            ppros.Height = count * 175;
            for (int i = 0; i < gdlist.Count; i++)
            {
                Pprocontrol pcon = new Pprocontrol();
                pcon.stype = "2";//下单
                pcon.cover = gdlist[i].cover;//封面
                pcon.describes = gdlist[i].describes;//描述
                pcon.gdname = gdlist[i].goods_name;//商品名称
                pcon.price = Convert.ToDouble(gdlist[i].price);//销售价
                pcon.market_price = "￥" + gdlist[i].market_price;//市场价
                pcon.gdid = gdlist[i].id;//商品ID
                ppros.Controls.Add(pcon);
            }

        }

        /// <summary>
        /// 移除全部按钮的样式
        /// </summary>
        public void RemoveAllStyle()
        {
            foreach (var con in pmenu.Controls)
            {

                Button btn = (Button)((MobileUserControl)con).Controls.Find("btnvalue", false);
                btn.BackColor = System.Drawing.Color.Transparent;
                btn.ForeColor = System.Drawing.Color.Black;
            }
        }
        /// <summary>
        /// 移除全部按钮的样式
        /// </summary>
        public void RemoveAllImgStyle1(string diskname)
        {
            disksn = diskname;//获取桌号名称
            pandisk.Width = 30;
            lblshow.Text = "<<";
            flag2 = false;
            pright.Width = 230;
            flag1 = true;
            lblshow1.Text = ">>";
            lbldiskname.Text = diskname;
            foreach (var con in pdisks.Controls)
            {

                Button btn = (Button)((MobileUserControl)con).Controls.Find("btndisk", false);
                btn.BackColor = System.Drawing.Color.White;
                btn.ForeColor = System.Drawing.Color.FromArgb(153, 153, 153);
            }
        }


        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <param name="count"></param>
        /// <param name="img"></param>
        public void Addpro(string name, string cover, double money, int num)
        {
            Gdpro pro = new Gdpro();
            pro.proimg = cover;
            pro.proname = name;
            pro.price = money;
            pro.count = num;
            int index = carlist.FindIndex(x => x.proname == name);
            if (index == -1)
                carlist.Add(pro);
            else
            {
                carlist[index].count = num;
            }

            if (carlist.Count > 0)
            {
                int count = 0;
                double price = 0;
                if (panpros.Controls.Count > 0)
                    panpros.Controls.Clear();
                panpros.Height = carlist.Count * 56;//设置panpro高度
                for (int i = 0; i < carlist.Count; i++)
                {

                    count += carlist[i].count;
                    price += (carlist[i].price * carlist[i].count);
                    ordprocontrol con = new ordprocontrol();
                    con.gdsname = carlist[i].proname;//商品名称
                    con.count = carlist[i].count.ToString();//数量
                    con.price = carlist[i].price.ToString();//价格
                    panpros.Controls.Add(con);
                }
                buycount = count;
                lblcount.Text = "数量：" + buycount.ToString();
                paymoney = price;//支付金额
                lblpaymoney.Text = "金额:￥" + price;//价格
            }
            else
            {
                buycount = 0;
                paymoney = 0;//支付金额
                lblcount.Text = buycount.ToString();
                lblpaymoney.Text = "金额：￥0.00";//价格
            }
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="name"></param>
        public void Delpro(string name)
        {
            int index = carlist.FindIndex(x => x.proname == name);
            if (index != -1)
            {
                if (carlist[index].count > 0)
                    carlist[index].count--;
                else if (carlist[index].count == 0)
                    carlist.RemoveAt(index);
            }
            if (carlist.Count > 0)
            {
                int count = 0;
                double price = 0;
                if (panpros.Controls.Count > 0)
                    panpros.Controls.Clear();
                panpros.Height = carlist.Count * 56;//设置panpro高度
                for (int i = 0; i < carlist.Count; i++)
                {

                    count += carlist[i].count;
                    price += (carlist[i].price * carlist[i].count);
                    ordprocontrol con = new ordprocontrol();
                    con.gdsname = carlist[i].proname;//商品名称
                    con.count = carlist[i].count.ToString();//数量
                    con.price = carlist[i].price.ToString();//价格
                    panpros.Controls.Add(con);
                }
                buycount = count;
                lblcount.Text = "数量：" + buycount.ToString();
                paymoney = price;//支付金额
                lblpaymoney.Text = "金额:￥" + price;//价格
            }
            else
            {
                if (panpros.Controls.Count > 0)
                    panpros.Controls.Clear();
                panpros.Height = 0;
                buycount = 0;
                paymoney = 0;//支付金额
                lblcount.Text = buycount.ToString();
                lblpaymoney.Text = "金额：￥0.00";//价格
            }
        }
        /// <summary>
        /// 下单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnfrim_Press(object sender, EventArgs e)
        {
            //MessageBox.Show("点击确定将完成订单", "下单提示", MessageBoxButtons.OKCancel, (obj, args) =>
            //{
            //    if (args.Result == ShowResult.OK)
            //    {
            ms_order ord = new ms_order();
            ord.order_sn = DateTime.Now.ToString("yyyyMMddHHmmssms");//订单号
            ord.pay_sn = DateTime.Now.ToString("yyyyMMddHHmmssms");//支付单号
            ord.pay_price = paymoney.ToString();//支付金额
            ord.discount_price = "0.00";//优惠金额
            ord.order_price = paymoney.ToString();//订单金额
            ord.integral = "0";//积分
            ord.integral_grant = "2";//未发放积分
            ord.pay_type = "4";//线下支付
            ord.status = "1";//未支付
            ord.orderdisksn = disksn;//桌号
            ord.codestatus = "2";//无需核销
            ord.user_id = null;
            ord.user_tel = null;//手机号
            ord.orderremark = "备注：" + txtremark.Text;//订单备注
            ord.add_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//下单时间
            ord.merchant_id = merchantid;//商家ID                   
            ord.goods_info = JsonConvert.SerializeObject(carlist);
            ord.is_online = "2";//线下订单
            ord.ordertype = "1";//普通订单
            ord.u_sage = u_sage;//到店
            ord.sales_id = adminid;//销售员ID
            if (orderDal.Addorder(ord) > 0)
            {
                order_markDal.Addrecord(ord.order_sn, ord.goods_info, merchantid, ord.pay_price);
                btnfrim.Enabled = false;
                // this.Close();
            }//创建订单
             //}
             //});
        }
        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnclear_Press(object sender, EventArgs e)
        {
            MessageBox.Show("点击确定将取消订单", "取消提示", MessageBoxButtons.OKCancel, (obj, args) =>
            {
                if (args.Result == ShowResult.OK)
                {
                    carlist.Clear();//清空购物车
                    panpros.Controls.Clear();
                    panpros.Height = 0;
                    lblcount.Text = "数量：0";
                    lblpaymoney.Text = "金额：￥0.00";
                    paymoney = 0;
                }
            });
        }
        /// <summary>
        /// 会员折扣
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btndist_Press(object sender, EventArgs e)
        {
            if (carlist.Count > 0)//如果存在购物车商品
            {
                Pdiscountcontrol con = new Pdiscountcontrol();
                con.merchantid = merchantid;
                this.Form.ShowDialog(con, (obj, args) =>
                {
                    if (con.discount_rate > 0)
                    {
                        double price = paymoney * con.discount_rate;
                        lbldiscount.Text = "优惠:￥" + (paymoney - price).ToString();
                        lblpaymoney.Text = "金额：￥" + price.ToString();
                    }

                });
            }
        }
        /// <summary>
        /// 消费扶零
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnzero_Press(object sender, EventArgs e)
        {
            if (paymoney > 1)
            {
                int price = (int)paymoney;
                paymoney = price;
                lblpaymoney.Text = "金额：￥" + price.ToString();
            }
        }
        bool flag1 = false;
        /// <summary>
        /// 打开已下单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pshow1_Press(object sender, EventArgs e)
        {
            if (flag1 == false)
            {
                flag1 = true;
                pright.Width = 230;
                lblshow1.Text = ">>";
            }
            else
            {
                flag1 = false;
                pright.Width = 30;
                lblshow1.Text = "<<";
            }
        }
        bool flag2 = false;
        private void pdshow_Press(object sender, EventArgs e)
        {
            if (flag2 == false)
            {
                flag2 = true;
                pandisk.Width = 230;
                lblshow.Text = ">>";
            }
            else
            {
                flag2 = false;
                pandisk.Width = 30;
                lblshow.Text = "<<";
            }
        }
    }
}